﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Gatheshop.Common;

namespace Gatheshop.Web.api.payment.giftcard
{
    public partial class WebForm1 : Web.UI.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //检查是否已登录
            Model.users userModel = new Web.UI.BasePage().GetUserInfo();
            if (userModel == null)
            {
                Response.Redirect(new Web.UI.BasePage().linkurl("payment", "login")); //尚未登录
                return;
            }
              //读取站点配置信息
            Model.siteconfig siteConfig = new BLL.siteconfig().loadConfig(GESKeys.FILE_SITE_XML_CONFING);
           
             BLL.gift_card bllGiftCard = new BLL.gift_card();
            string cardNo=this.txtCardNo.Text;
            string cardPwd=this.txtCardPwd.Text;
            decimal card_amount = bllGiftCard.getCardcount(cardNo,cardPwd);

            if (card_amount == 0 )
            {
                Response.Redirect(siteConfig.webpath + "error.aspx?msg=" + Utils.UrlEncode("对不起，您提交的卡号、密码有误，或者已经激活！请和客服联系."));
                return;
            }


            int amountNewId = new BLL.amount_log().Add(userModel.id, userModel.user_name, GEShopEnums.AmountTypeEnum.Recharge.ToString(), card_amount, "账户充值(礼品卡兑换:" + cardNo + ")", 1);
             if (amountNewId < 1)
             {
                 Response.Write("{\"msg\":0, \"msgbox\":\"充值过程中发生错误，请重新提交！\"}");
                 return;
             }
             else
             {
                 Boolean IsSuccess =bllGiftCard.setActive(cardNo, cardPwd, userModel.user_name); 
                 //兑换成功
                if (IsSuccess == true)
                {
                    //Response.Write("{\"msg\":0, \"msgbox\":\"充值成功！\"}");
                    Type cstype = this.GetType();

                        ClientScript.RegisterStartupScript(cstype, "key", "<script>window.alert('充值成功！');window.location.href='/user/amount/recharge.aspx';</script>");
                    //Response.Redirect(siteConfig.webpath + "user/amount/recharge.aspx");
                    return;
                }
                else
                {
                    Response.Redirect(siteConfig.webpath + "error.aspx?msg=" + Utils.UrlEncode("对不起，充值过程中发生错误，请和客服联系！"));
                    return;
                }
                 
  
             }


        }
        }
  
}